<template>
  <div id="app">
    <!--    <router-view v-wechat-title="$route.meta.title"></router-view>-->
    <keep-alive>
      <router-view v-wechat-title="$route.meta.title" v-if="$route.meta.keepAlive">
        <!-- 这里是会被缓存的视图组件，比如列表A页面 -->
      </router-view>
    </keep-alive>

    <router-view v-wechat-title="$route.meta.title" v-if="!$route.meta.keepAlive">
      <!-- 这里是不被缓存的视图组件，比如详情B页面-->
    </router-view>
  </div>
</template>

<script>
  import {userLoginByTokenUri} from './common/api';

  export default {
    name: 'app',
    data() {
      return {}
    },
    methods: {
      login() {
        const access_token = window.localStorage.getItem('access_token');
        if (!access_token) {
          return;
        }
        const that = this;
        this.$http.get(userLoginByTokenUri + '?access_token=' + access_token).then(response => {
          let {code, msg, data} = response.body
          if (code != 0) {
            alert(msg);
          } else {
            that.GLOBAL.userInfo = data.user;
            that.EventBus.$emit('OnUserLogined', null);
            that.$router.push({name: 'Main'});
          }
        }, response => {
          // error callback
        })
      }
    },
    beforeCreate: function () {
//      console.log('App beforeCreate 钩子执行...')
    },
    created: function () {
//      console.log('App created 钩子执行...')
      if(!this.GLOBAL.userInfo) {
        this.login();
      }
    },
    beforeMount: function () {
//      console.log('App beforeMount 钩子执行...')
    },
    mounted: function () {
//      console.log('App mounted 钩子执行...')
    },
    beforeUpdate: function () {
//      console.log('App beforeUpdate 钩子执行...')
    },
    updated: function () {
//      console.log('App updated 钩子执行...')
    },
    beforeDestroy: function () {
//      console.log('App beforeDestroy 钩子执行...')
    },
    destroyed: function () {
//      console.log('App destroyed 钩子执行...')
    }
  }
</script>

<style>
  html, body, #app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #2c3e50;
    font-size: 16px;
    height: 100%;
  }
</style>
